package de.pierrepoenisch.checkyoursite.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

public class CheckResultProvider {
	
	private final CheckSiteServiceAsync checkSiteService;
	private CheckYourSite checkYourSiteView;
	
	public CheckResultProvider(CheckYourSite view){
		//save the view
		checkYourSiteView = view;
		
		//initialize the service
		checkSiteService = (CheckSiteServiceAsync) GWT.create(CheckSiteService.class);
		ServiceDefTarget target = (ServiceDefTarget) checkSiteService;
		//service url as defined in checkyoursite.gwt.xml
		String relServiceURL = GWT.getModuleBaseURL() + "checkSite";
		target.setServiceEntryPoint(relServiceURL);
	}
	
	public void getCheckResult(String URI){
		
		checkSiteService.checkSite(URI, new AsyncCallback(){

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			public void onSuccess(Object result) {
				CheckResult checkResult = (CheckResult) result;
				processCheckResult(checkResult);
			}
		});
		
	}

	private void processCheckResult(CheckResult checkResult) {
		//here we can do something with the result
		
		//and finaly show it to the user
		checkYourSiteView.showCheckResult(checkResult);
	}
}
